<?php
/**
 * Created by PhpStorm.
 * User: lili
 * Date: 2018/12/30
 * Time: 12:53
 */

namespace system\core\route;

/**
 *url解析类
 *
 **/
class Route
{
    public static $instance  ;

    protected  $request;

    protected $routearr;

    ///构造函数，根据参数数量选择对应方法
    protected function __construct()
    {
        $this->request =  \system\core\route\Request::getInstance();//注入request
        $this->parseRequest($this->request);
    }
   
    
    /**
     * 初始化
     * @access public
     * @param array $options 参数
     * @return \system\core\route\Request
     */
    public static function getInstance()
    {

        if (empty(self::$instance)) {
            self::$instance = new self();
        }
//        var_dump(self::$instance);
        return self::$instance;
    }

    /*
     * 解析request
     * @param Request $request 对象
     * */
    public function parseRequest(Request $request){
        $routearr =  $this->checkurl($request);
        if (!empty($routearr)){
             $this->routearr =$routearr;
        }
        
         

    }


    /*
     * 监测url
     * @param string $path
     *
     *
     * */
    public function checkurl(Request $request){
        $getin = $request->input('','get');
        if ( empty($getin['m']) || empty($getin['c']) || empty($getin['a']) ){
            if (empty($getin['m'])){
                throw new \Exception('模块不存在');
            }
            if (empty($getin['c'])){
                throw new \Exception('控制器不存在');
            }
            if (empty($getin['a'])){
                throw new \Exception('方法不存在');
            }
        }
        $routearr['m'] = $getin['m'];
        $routearr['c'] = $getin['c'];
        $routearr['a'] = $getin['a'];
        
        return $routearr;
        
    }

    /*
     * 获得模块
     * 
     * */
    public function getRoutearr(){
        if ( !empty($this->routearr) ){
            return $this->routearr;
        }
        return false;
    }
    
    /*
     * 获得模块
     * 
     * */
    public function getMoudule(){
        if ( !empty($routearr = $this->checkurl($this->request)) ){
            return $routearr['m'];
        }
        
    }
    /*
     * 获得控制器
     * 
     * */
    public function getController(){
        if ( !empty($routearr = $this->checkurl($this->request)) ){
            return $routearr['c'];
        }

    }
    /*
     * 获得方法
     * 
     * */
    public function getAction(){
        if ( !empty($routearr = $this->checkurl($this->request)) ){
            return $routearr['a'];
        }
        
    }



}